package com.korion.kdyassistant.ui

import android.content.Intent
import android.os.Bundle
import android.provider.Settings
import android.view.View
import android.view.Window
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import com.korion.kdyassistant.BuildConfig
import com.korion.kdyassistant.R
import com.korion.kdyassistant.databinding.ActivityMainBinding
import com.korion.kdyassistant.utils.DeviceUtils
import com.korion.kdyassistant.utils.PermissionUtils


class MainActivity : AppCompatActivity(), View.OnClickListener {

    companion object {
        const val TAG = "MainActivity"
    }

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE)
        window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
        window.statusBarColor = getColor(R.color.transparent)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.tvVersion.text = String.format(getString(R.string.version, BuildConfig.VERSION_NAME))

        if (!Settings.canDrawOverlays(this) || !PermissionUtils.isAccessibilityServiceEnabled(this)){
            AlertDialog.Builder(this)
                .setTitle(R.string.permission_request)
                .setMessage(R.string.need_permission_desc)
                .setNegativeButton(R.string.cancel){ dialog, _ ->
                    dialog.cancel()
                }
                .setPositiveButton(R.string.confirm){ dialog, _ ->
                    PermissionUtils.checkSystemWindowPermission(this)
                            && PermissionUtils.checkAccessibilityService(this)
                }.show()
        }

        binding.tvVersion.setOnLongClickListener {
            DeviceUtils.enterEngineerMode(this@MainActivity)
            return@setOnLongClickListener true
        }

        binding.btnButtonMapped.setOnClickListener(this)
        binding.btnAutoClick.setOnClickListener(this)
    }

    override fun onClick(v: View) {
        when(v.id) {
            R.id.btn_button_mapped -> {
                val intent = Intent(this, ButtonMappedActivity::class.java)
                startActivity(intent)
            }
            R.id.btn_auto_click -> {
                val intent = Intent(this, AutoClickActivity::class.java)
                startActivity(intent)
            }
        }
    }

}
